import * as types from './types';
import {combineReducers} from 'redux';

function userInfo(state = {}, action) {
    if (action.type === types.RECEIVE_USERINFO) {
        return Object.assign({}, state, action.payload);
    }

    if (action.type === types.USER_DID_SIGNOUT) {
        return {};
    }

    return state;
}

const initialAuthState = {
    isAuthenticated: false,
};

function auth(state = initialAuthState, action) {
    if (action.type === types.USER_DID_SIGNIN) {
        return {
            isAuthenticated: true,
        };
    }

    if (action.type === types.USER_DID_SIGNOUT) {
        return {
            isAuthenticated: false,
        };
    }
    return state;
}

function queues(state = [], action) {
    if (action.type === types.RECEIVE_QUEUES) {
        return [...action.payload];
    }

    if (action.type === types.ADD_QUEUE) {
        return [...state, {...action.payload}];
    }

    if (action.type === types.REMOVE_QUEUE) {
        return state.filter(queue => queue.id !== action.payload.id);
    }

    if (action.type === types.RESET_QUEUE) {
        return [];
    }

    return state;
}

function completedQueues(state = [], action) {
    if (action.type === types.RECEIVE_COMPLETED_QUEUES) {
        return [...action.payload];
    }

    if (action.type === types.ADD_COMPLETED_QUEUE) {
        return [...state, {...action.payload}];
    }

    if (action.type === types.REMOVE_COMPLETED_QUEUE) {
        return state.filter(queue => queue.id !== action.payload.id);
    }

    if (action.type === types.RESET_COMPLETED_QUEUE) {
        return [];
    }
    return state;
}

function holdonQueues(state = [], action) {
    if (action.type === types.RECEIVE_HOLDON_QUEUES) {
        return [...action.payload];
    }

    if (action.type === types.ADD_HOLDON_QUEUE) {
        return [...state, {...action.payload}];
    }

    if (action.type === types.REMOVE_HOLDON_QUEUE) {
        return state.filter(queue => queue.id !== action.payload.id);
    }

    if (action.type === types.RESET_HOLDON_QUEUE) {
        return [];
    }

    return state;
}

function orderStatuses(state = [], action) {
    if (action.type === types.RECEIVED_ORDER_STATUSES) {
        return [...action.payload];
    }

    return state;
}

function variantMaps(state = {}, action) {
    if (action.type === types.RECEIVE_VARIANT_MAPS) {
        return {...action.payload};
    }

    return state;
}

function variants(state = {}, action) {
    if (action.type === types.RECEIVE_VARIANTS) {
        return {...action.payload};
    }

    return state;
}

export default combineReducers({
    auth,
    userInfo,
    variants
});
